package com.xyhy.starter.web.hutool;

import cn.hutool.core.convert.AbstractConverter;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneOffset;
import java.util.Calendar;

/**
 * @author robert
 * @since 2025-03-04
 */
@Slf4j
public class LocalDateConverter extends AbstractConverter<LocalDate> {
  private String format = "yyyy-MM-dd";

  @Override
  protected LocalDate convertInternal(Object value) {
    long mills = 0L;
    if (value instanceof Calendar) {
      mills = ((Calendar) value).getTimeInMillis();
    }
    if (value instanceof Long) {
      mills = (Long) value;
    }
    String valueStr = convertToStr(value);
    try {
      mills = StrUtil.isBlank(this.format) ? DateUtil.parse(valueStr).getTime() : DateUtil.parse(valueStr,
              this.format).getTime();
    } catch (Exception var6) {
      log.error(var6.getMessage(), var6);
    }
    if (0L == mills) {
      return null;
    } else {
      return LocalDateTime.ofEpochSecond(mills, 0, ZoneOffset.ofHours(8)).toLocalDate();
    }
  }
}
